#include "head.h"

int mkdirFunc(const char* dir1,stack1_t pdir){
    char str[1024];
    strcpy(str,dir1);
    int idx=0;
    char* p[1024];
    p[idx++]=strtok(str," ");
    while((p[idx++]=strtok(NULL," "))!=NULL);
    char subpath[256]={0};
    int ret=0;
    for(int i=0;i<idx && p[i]!=NULL;i++){
        onePath(p[i],subpath,&pdir);
        mode_t mode=0777;
        ret=mkdir(subpath,mode);
        if(ret==-1){
            printf("%s mkdir error\n",subpath);
            return -1;
        }
    }
    return 0;
}
int removeFunc(const char* filename,stack1_t pdir){
    char str[1024];
    strcpy(str,filename);
    int idx=0;
    char* p[1024];
    p[idx++]=strtok(str," ");
    while((p[idx++]=strtok(NULL," "))!=NULL);
    char subpath[256]={0};
    int ret=0;
    for(int i=0;i<idx&& p[i]!=NULL;i++){
        onePath(p[i],subpath,&pdir);
        int retval=unlink(subpath);
        if(retval==-1){
            printf("unlink is error");
            ret=-1;
        }
    }
    return ret;
}

int rmdirFunc(const char* filename,stack1_t pdir){
    char str[1024];
    strcpy(str,filename);
    int idx=0;
    char* p[1024];
    p[idx++]=strtok(str," ");
    while((p[idx++]=strtok(NULL," "))!=NULL);
    char subpath[256]={0};
    int ret=0;
    for(int i=0;i<idx&& p[i]!=NULL;i++){
        onePath(p[i],subpath,&pdir);
        int retval=rmdir(subpath);
        if(retval==-1){
            printf("rmdir is error");
            ret=-1;
        }
    }
    return ret;
}
